<?php

namespace app\home\controller\post;

use app\common\controller\Home;

class Post extends Home
{
    public function __construct()
    {
        parent::__construct();

        //帖子模型
        $this->PostModel = model('Post.post');

        //用户模型
        $this->UserModel = model('User.User');

        //消费模型
        $this->RecordModel = model('User.Record');
    }

    //添加帖子
    public function add()
    {

        if ($this->request->isPost()) {
            //接收所有数据
            $params = $this->request->param();

            //接受验证码
            $vercode = $this->request->param('vercode', '');

            //判断验证码是否正确
            if (!captcha_check($vercode)) {
                $this->error('验证码错误');
                exit;
            }

            //附加功能下拉菜单里面的value值
            $state = $this->request->param('state', 0);

            //根据$state获取附加功能相应的积分
            $statepoint = model('Config')->where('key', "Poststate{$state}")->value('value');

            //悬赏积分
            $point = $this->request->param('point', 0);

            //需要发帖人付出的总积分  bcadd() — 两个任意精度数字的加法计算
            $UpdatePoint = bcadd($statepoint, $point);

            //获取到用户的所有积分
            $UserPoint = $this->LoginUser['point'];

            //用户的积分减去发帖所需的积分 bcsub — 两个任意精度数字的减法
            $Point = bcsub($UserPoint, $UpdatePoint);

            //积分不足时,提示
            if ($Point < 0) {
                $this->error('积分不足,请充值');
                exit;
            }

            //插入帖子表 => 插入消费记录表 => 更新用户表

            //开启事务
            $this->PostModel->startTrans();
            $this->UserModel->startTrans();
            $this->RecordModel->startTrans();

            //封装帖子的数据
            $PostData = [
                'title' => $params['title'],
                'content' => $params['content'],
                'point' => $UpdatePoint,
                'state' => $state,
                'userid' => $this->LoginUser['id'],
                'cateid' => $params['cateid'],
            ];

            //插入帖子表
            $PostStatus = $this->PostModel->validate('common/Post/Post')->save($PostData);

            if ($PostStatus == FALSE) {
                $this->error($this->PostModel->getError());
                exit;
            }

            //封装消费的数据
            $RecordData = [
                'point' => $UpdatePoint,
                'state' => 1,
                'userid' => $this->LoginUser['id']
            ];

            //判断消费状态
            switch ($state) {
                case 1:
                    $RecordData['content'] = "【" . $PostData['title'] . "】采纳积分:<b>" . $point . "积分</b> 置顶：<b>{$statepoint}积分</b> - 发布时间：" . date("Y-m-d H:i");
                    break;
                case 2:
                    $RecordData['content'] = "【" . $PostData['title'] . "】采纳积分:<b>" . $point . "</b>积分 精华：<b>{$statepoint}积分</b> - 发布时间：" . date("Y-m-d H:i");
                    break;
                case 3:
                    $RecordData['content'] = "【" . $PostData['title'] . "】采纳积分:<b>" . $point . "</b>积分 热门：<b>{$statepoint}积分</b> - 发布时间：" . date("Y-m-d H:i");
                    break;
                default:
                    $RecordData['content'] = "【" . $PostData['title'] . "】采纳积分:<b>" . $point . "</b>积分 - 发布时间：" . date("Y-m-d H:i");
                    break;
            }

            //封装用户更新积分数据
            $UserData = [
                'id' => $this->LoginUser['id'],
                'point' => $Point
            ];

            //数据插入消费表
            $ReacorStatus = $this->RecordModel->validate('common/user/Record')->save($RecordData);

            if ($ReacorStatus == FALSE) {
                //将帖子表插入的数据回滚
                $this->PostModel->rollback();
                $this->error($this->ReacorModel->getError());
                exit;
            }

            //更新用户的积分
            $UserStatus = $this->UserModel->isUpdate(true)->save($UserData);

            if ($UserStatus == FALSE) {
                $this->PostModel->rollback();
                $this->RecordModel->rollback();

                $this->error($this->UserModel->getError());
                exit;
            }

            //终极判断
            if ($PostStatus === FALSE || $ReacorStatus === FALSE || $UserStatus === FALSE) {
                $this->PostModel->rollback();
                $this->RecordModel->rollback();
                $this->UserModel->rollback();
                $this->error('帖子发布失败，请重新操作');
                exit;
            } else {
                //提交事务
                $this->UserModel->commit();
                $this->RecordModel->commit();
                $this->PostModel->commit();
                $this->success('帖子发布成功', url('home/index/index'));
                exit;
            }
        }

        //查询分类
        $CateList = model('Cate')->order('weigh', 'asc')->select();

        //查询附加功能的选项
        $stateList = $this->PostModel->state();

        //赋值
        $this->assign([
            'CateList' => $CateList,
            'stateList' => $stateList
        ]);

        return $this->fetch();
    }
}
